package com.swpu.service_knowledge.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.service_knowledge.entity.KnowledgeArticle;
import com.swpu.service_knowledge.entity.vo.ArticleCollectVo;
import com.swpu.service_knowledge.entity.vo.KnowledgeArticleVo;
import com.swpu.service_knowledge.service.KnowledgeArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-03-27
 */
@RestController
@RequestMapping("/service_knowledge/knowledge-article")
public class KnowledgeArticleController {

    @Autowired
    private KnowledgeArticleService knowledgeArticleService;
    @GetMapping("getKnowledgeArticleByCategoryId")
    public R getKnowledgeArticleByCategoryId(@RequestParam("categoryId") Integer categoryId){
        List<KnowledgeArticle> articleList = knowledgeArticleService.getKnowledgeArticleByCategoryId(categoryId);
        return R.ok().data("articleList",articleList);
    }

    @GetMapping("getKnowledgeArticleById")
    public R getKnowledgeArticleById(@RequestParam("id") Integer id){
        KnowledgeArticle knowledgeArticle = knowledgeArticleService.getKnowledgeArticleById(id);
        return R.ok().data("article",knowledgeArticle);
    }
    @GetMapping("getKnowledgeArticleByTitle")
    public R getKnowledgeArticleById(@RequestParam("title") String  title){
        List<KnowledgeArticle> articleList  = knowledgeArticleService.getKnowledgeArticleByTitle(title);
        return R.ok().data("articleList",articleList);
    }


    @GetMapping("getHotKnowledgeArticle")
    public R getKnowledgeArticleByCategoryId(){
        List<KnowledgeArticle> hotArticleList = knowledgeArticleService.getHotKnowledgeArticle();
        return R.ok().data("hotArticleList",hotArticleList);
    }
    @GetMapping("getUserArticleCollectList")
    public R getUserArticleCollectList(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        List<ArticleCollectVo> articleCollectList = knowledgeArticleService.getUserArticleCollectList(openid);
        return R.ok().data("articleCollectList",articleCollectList);
    }


}

